package com.wteam;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wteam.PoJo.DTO.order_goods;
import com.wteam.PoJo.DTO.ordernumbertwo;
import com.wteam.mapper.OrderGoodsMapper;
import com.wteam.mapper.OrdernumbertwoMapper;
import com.wteam.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class Task {
    @Autowired
    private OrdernumbertwoMapper ordernumbertwoMapper;
    //
//    @Autowired
//    private RedisTemplate redisTemplate;
    @Autowired
    private OrderService orderService;
    @Autowired
private OrderGoodsMapper orderGoodsMapper;

    //每分钟检查一次订单状态
    @Scheduled(cron = "0 * * * * ?")
    public void OrderStatus() {
        LocalDateTime ChangeOrderTiem = LocalDateTime.now().plusMinutes(-15);

        LambdaQueryWrapper<ordernumbertwo> orderstatus = new LambdaQueryWrapper();
        orderstatus.eq(ordernumbertwo::getOrderStatus, 1)
                .le(ordernumbertwo::getCreateTime, ChangeOrderTiem);
        List<ordernumbertwo> ordernumbertwos = ordernumbertwoMapper.selectList(orderstatus);
        log.info(ordernumbertwos.toString());
        if (ordernumbertwos != null && ordernumbertwos.size() != 0) {
            for (ordernumbertwo ordernumbertwo : ordernumbertwos) {
                ordernumbertwo.setOrderStatus(3);
                ordernumbertwoMapper.updateorderstatus(ordernumbertwo.getId());
                //把订单商品状态改一下
                LambdaQueryWrapper<order_goods> orderGoodsLambdaQueryWrapper
                        = new LambdaQueryWrapper<>();
                orderGoodsLambdaQueryWrapper.eq(order_goods::getOrdersnId, ordernumbertwo.getSn());
                List<order_goods> order_goods = orderGoodsMapper.selectList(orderGoodsLambdaQueryWrapper);
                for (com.wteam.PoJo.DTO.order_goods orderGood : order_goods) {
                    orderGood.setAfterSaleStatus(5);
                    orderGoodsMapper.updateorderGood(orderGood.getId());
                }
            }
        }

    }

}
